Adds snippets and tests for documentation tutorial.#729
Conversation
language/tutorial/tutorial.py
Outdated
|
|
||
|
|
||
| def authenticate(): | ||
| '''Authenticates the client library using default application |
There was a problem hiding this comment.
Use double quotes for docstrings, please.
language/tutorial/tutorial.py
Outdated
| return service | ||
|
|
||
|
|
||
| def getResponse(filename): |
There was a problem hiding this comment.
snake_case for everything but class names, please.
language/tutorial/tutorial.py
Outdated
| # [END import_libraries] | ||
|
|
||
|
|
||
| def authenticate(): |
There was a problem hiding this comment.
We almost universally avoid having a separate function construct the client. Indirection can be confusing for users. This is two lines of code, would you mind just in-lining this in the relevant functions?
There was a problem hiding this comment.
On second thought since this is part of a cohesive tutorial and not snippets this can stay. But can you rename it to something like make_client?
There was a problem hiding this comment.
Done. I ended up removing the separate function, it may actually make sense to flatten the whole sample into a single helper that accepts a filename.
language/tutorial/tutorial.py
Outdated
| # [START parsing_the_response] | ||
| score = response['documentSentiment']['score'] | ||
| magnitude = response['documentSentiment']['magnitude'] | ||
| for i, sentence in enumerate(response['sentences']): |
There was a problem hiding this comment.
Nit: use n when using enumerate.
language/tutorial/tutorial_test.py
Outdated
| def test_neutral(): | ||
| result = tutorial.getResponse('reviews/bladerunner-neutral.txt') | ||
| assert result['language'] == 'en' | ||
| assert (result['documentSentiment']['score'] > -1 and |
There was a problem hiding this comment.
Python trick for you:
assert -1 < result['documentSentiment']['score'] < 1
language/tutorial/tutorial.py
Outdated
| import argparse | ||
|
|
||
| from googleapiclient import discovery | ||
|
|
There was a problem hiding this comment.
No empty line between these two.
language/tutorial/tutorial.py
Outdated
| service = discovery.build('language', 'v1', credentials=credentials) | ||
| # [END authenticating_to_the_api] | ||
| # [START constructing_the_request] | ||
| with open(filename, 'r') as review_file: |
There was a problem hiding this comment.
- Put an empty newline above control statements
- You didn't limit the scope of the with statement as I mentioned in the last round. :(
| # [START parsing_the_response] | ||
| score = response['documentSentiment']['score'] | ||
| magnitude = response['documentSentiment']['magnitude'] | ||
| for n, sentence in enumerate(response['sentences']): |
There was a problem hiding this comment.
blank newline above control statements.
| sentence_sentiment = sentence['sentiment']['score'] | ||
| print('Sentence {} has a sentiment score of {}'.format(n, | ||
| sentence_sentiment)) | ||
| print('Overall Sentiment: score of {} with magnitude of {}'.format( |
There was a problem hiding this comment.
Blank newline above this statement, please.
language/tutorial/tutorial.py
Outdated
|
|
||
|
|
||
| # [START running_your_application] | ||
| def main(filename): |
There was a problem hiding this comment.
You can remove this function now.
Adds code to back up documentation. Note that on my machine, there are issues with generating the documentation so that still should be added before merging.